package com.qfedu.controller;

import com.qfedu.entity.DepartNameWithStaffNum;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.Staff;
import com.qfedu.entity.StaffNumDate;
import com.qfedu.entity.dataList;
import com.qfedu.service.StaffService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
//@RequestMapping("/StaffController")
//@RequestMapping
public class StaffController {
    @Autowired
    private StaffService staffService;

    @RequestMapping("/addStaff")
    @ResponseBody
    public ResponseData addStaff(Staff staff,HttpSession session){

        String filepath = (String) session.getAttribute("filepath");

        staff.setHeading(filepath);

        boolean result = staffService.addStaff(staff);

        ResponseData responseData = new ResponseData();

        if(result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
            session.setAttribute("filepath", "");
            return responseData;
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
            return responseData;
        }



    }

    @RequestMapping("/uploadFile")
    @ResponseBody
    public ResponseData uploadFile(String filename, List<MultipartFile> files, HttpSession session) throws IOException {

        System.out.println("filename:"+filename);

        if (files!=null && files.size()>0){


            for (MultipartFile multipartFile:files){
                // 循环获取文件
                String filePath = "D:\\项目\\三期项目\\dagongren\\src\\main\\webapp\\media\\images";
                File file = new File(filePath);
                if (!file.exists()){
                    //创建文件
                    file.mkdirs();
                }
                // 或取文件名
                String originName =   UUID.randomUUID()+"_" + multipartFile.getOriginalFilename();

                String uploadFile =    filePath+ File.separator +  originName;
                // 讲接收到的文件数据保存
                multipartFile.transferTo(new File(uploadFile));

                File oldFile = new File(uploadFile);
                File newFile = new File("D:\\项目\\三期项目\\dagongren\\target\\qfedu-office\\media\\images");
                FileUtils.copyFileToDirectory(oldFile,newFile);
                String filepath = "\\media\\images\\"+originName;
                session.setAttribute("filepath",filepath);
            }
        }
        ResponseData responseData = new ResponseData();

        responseData.setMsg("ok");
        responseData.setCode(1);
        return responseData;
    }



    @RequestMapping("/findAllStaff")
    @ResponseBody
    public ResponseData findAllStaff(int page,int limit){

        return staffService.findAllStaff(page,limit);

    }


    @RequestMapping("/deleteStaffByuNo")
    @ResponseBody
    public ResponseData deleteStaffByuNo(String uNo){

        boolean s = staffService.deleteStaffByuNo(uNo);


        ResponseData responseData = new ResponseData();
        if (s){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }


    /**
     * 各部门人数折线图
     * @return
     */
    @RequestMapping("/getDepartWithStaff")
    @ResponseBody
    public ResponseData getDepartNameWithStaffNum(){
        ResponseData<DepartNameWithStaffNum> responseData = new ResponseData<>();
        DepartNameWithStaffNum departNameWithStaffNum = staffService.getDepartNameWithStaffNum();

        System.out.println(departNameWithStaffNum);

        responseData.setData(departNameWithStaffNum);
        responseData.setMsg(ResponseData.SUCCESS_MSG);
        return responseData;
    }

    @RequestMapping("/getStaffNumData")
    @ResponseBody
    public ResponseData<StaffNumDate> getStaffNumData(){
        StaffNumDate staffNumData = staffService.getStaffNumData();

        ResponseData<StaffNumDate> responseData = new ResponseData<>();

        responseData.setCode(ResponseData.SUCCESS_CODE);
        responseData.setMsg(ResponseData.SUCCESS_MSG);
        responseData.setData(staffNumData);

        return responseData;
    }





}
